﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebClient.clientCtrls
{
    public partial class comfirm : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            this.btnMuaHang.Attributes.Add("onclick", "return isCheck();");
            if (!this.IsPostBack) {
                if (Session["users"] != null)
                {
                    DTO.Users item = (DTO.Users)Session["users"];
                    this.fullname.Text = item.fullName;
                    this.email.Text = item.email;
                    this.dienthoai.Text = item.phone;
                    this.diachi.Text = item.address;
                    if (Session["shopping"] != null)
                    {
                        System.Data.DataTable tb = (System.Data.DataTable)Session["shopping"];
                        this.grdDs.DataSource = tb;
                        this.grdDs.DataBind();
                        Decimal sum = 0;
                        foreach (DataRow row in tb.Rows)
                        {
                            Decimal tt = Convert.ToDecimal(row["thanhtien"]);
                            sum += tt;
                        }
                        lblSum.Text = sum.ToString();
                    }
                }
                else {
                    Session.Remove("shopping");
                    Response.Redirect("/Action.aspx");
                }
                
                
            }
        }

        protected void btnMuaHang_Click(object sender, ImageClickEventArgs e)
        {
            if (!WebClient.Check.checkCard(txtCard.Text)) {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + "Xin vui long nhap lai card " + "');", true);
                txtCard.Focus();
                return;
            }
            if (Session["users"] != null)
            {
                Decimal tt = 0;
                Decimal tygia = 20900;
                try {
                    tt = Convert.ToDecimal(lblSum.Text);
                }
                catch { }
                DTO.Users item = (DTO.Users)Session["users"];
                item.fullName = this.fullnamez.Text;
                item.email = this.emailz.Text;
                item.phone = this.dienthoaiz.Text;
                item.address = this.diachiz.Text;
                DTO.Cart cartItem = new DTO.Cart();
                cartItem.id = 1;
                cartItem.fullName = item.fullName;
                cartItem.address = this.diachiz.Text;
                cartItem.arrears = 0;
                cartItem.create_on = DateTime.Now;
                cartItem.credit = txtCard.Text ;//"5232452345834348";
                cartItem.customersId = item.id;
                cartItem.discount = 0;
                cartItem.isActive = 1;
                cartItem.orderId = 1;
                cartItem.payment = tt;
                cartItem.phone = this.dienthoaiz.Text;
                cartItem.status = 0;//chua giao
                cartItem.total = tt;
                WebData db = new WebData();
                db.OnSubmitCart(ref cartItem, 0);
                if (Session["shopping"] != null)
                {
                    System.Data.DataTable tb = (System.Data.DataTable)Session["shopping"];
                    foreach (DataRow row in tb.Rows)
                    {
                        //Decimal tt = Convert.ToDecimal(row["thanhtien"]);
                        //sum += tt;
                        DTO.Cartdetail cartDetail = new DTO.Cartdetail();
                        cartDetail.cartId = cartItem.id;
                        cartDetail.discount = 0;
                        cartDetail.productId = Convert.ToInt32(row[DTO.Products.c_id]);
                        cartDetail.quality = Convert.ToInt32(row["soluong"]);
                        cartDetail.rate = tygia;
                        cartDetail.unitPriceUSD = Convert.ToDecimal(row[DTO.Products.c_price]);
                        cartDetail.cash = cartDetail.quality * cartDetail.unitPriceUSD;
                        cartDetail.unitPriceVND = cartDetail.unitPriceUSD * tygia * cartDetail.quality;
                        cartDetail.orderId = 1;
                        db.OnSubmitCartdetail(cartDetail, 0);
                    }
                    
                    Response.Redirect("/carts.aspx");
                }

            }
        }

       
    }
}